import { nmAppErrorLog } from '@app-nextmesh/common/utils';
import { showLoading } from '@app-nextmesh/components';
import { documentV2 } from '@app-nextmesh/domain';
import { useProjectStore } from '@app-nextmesh/store/project';
import AlertDialog from '../AlertDialog';

interface IProps {
  uuid: string;
  onOk: () => void;
  onClose: () => void;
}
export default function ResultAlertDialog({ uuid, onOk, onClose }: IProps) {
  const { projectInfo, setProjectInfo } = useProjectStore();
  const doDelete = async () => {
    const hide = showLoading();
    try {
      await documentV2.deleteResult(uuid);
      setProjectInfo({
        ...projectInfo,
        results: projectInfo.results.filter((result) => result.uuid !== uuid),
      });
      onOk();
      onClose();
    } catch (error) {
      nmAppErrorLog('SimulationAlertDialog', error);
    } finally {
      hide();
    }
  };
  return (
    <AlertDialog
      alert='确定删除这个结果吗？删除结果将删除所有相关数据，无法恢复。'
      primaryTitle='确定删除'
      onPrimaryClick={doDelete}
      onSecondaryClick={onClose}
    />
  );
}
